﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//using BLToolkit.Data;
using BLToolkit.Data.Linq;
//using BLToolkit.DataAccess;

namespace Paaboo.Todo.Library
{
    public class ProjectBO
    {
        /// <summary>
        /// 添加项目
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static bool Add(long userId, string title)
        {
            object result = TaskDB.GetTable<ProjectEN>()
                .InsertWithIdentity(() => new ProjectEN
            {
                Title = title,
                UserId = userId,
            });
            return (result != null);
        }
        /// <summary>
        /// 获取用户项目列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static List<ProjectEN> GetCatalogList(long userId)
        {
            return TaskDB.GetTable<ProjectEN>()
                .Where(o => o.UserId == userId)
                .OrderBy(o => o.ProjectId)
                .ToList();
        }
        /// <summary>
        /// 编辑项目
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="catalogId"></param>
        /// <param name="newTitle"></param>
        /// <returns></returns>
        public static bool Edit(long userId, int catalogId, string newTitle)
        {
            int result = TaskDB.GetTable<ProjectEN>()
                .Where(o => o.ProjectId == catalogId && o.UserId == userId)
                .Set(o => o.Title, newTitle)
                .Update();

            return result > 0;
        }
        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="catalogId"></param>
        /// <returns></returns>
        public static bool Delete(long userId, int catalogId)
        {
            TaskDB.GetTable<ProjectEN>()
               .Where(o => o.ProjectId == catalogId && o.UserId == userId)
               .Set(o => o.Status, Status.Deleted)
               .Update();
            return true;
        }
    }
}
